<?php
/**
 * Created by PhpStorm.
 * User: fang_
 * Date: 2016/10/1
 * Time: 10:58
 */
require 'vendor/autoload.php';
require 'sendnotifitionmail.php';
$client = new GuzzleHttp\Client();


//print_r($_POST);
if (!empty($_POST['hwaid']) && !empty($_COOKIE['session_id'])) {

    if ($_POST['sendnotification'] == 'mail')
    {
        $stu_id = $_POST['stuid'];
        // 读取包含学号对应学生信息的文本文件
        // 提取出姓名和邮箱
        $stu_name = $_POST['stuname'];
        $mail_address = getEmail($stu_name);
        if ($mail_address != false)
        {
            $body = $stu_name."同学你好，本次作业的结果如下：</br>".'批阅: '.urldecode($_POST['IPT_BODY'])."</br>".'评语: '.
                urldecode($_POST['comments']).'</br>'.'评分: '.$_POST['mark']."/30.0</br></br>"."助教-方文达&任课教师-姚争为";

            sendMail($mail_address,$stu_name,$_POST['labname'],$body);
        }

    }

    $res = $client->request('POST',
        'http://e-learning.hznu.edu.cn/eol/common/hw/hwanswer.comment.do.jsp',
//    'http://httpbin.org/post',
        [
            'headers' => ['Cookie' => $_COOKIE['session_id']],
            'form_params' => [
                'comments'=>urldecode(iconv('UTF-8','GB2312',$_POST['comments'])),
                'fullmark'=> '30.0',
               'hwaid'=> $_POST['hwaid'],
               'IPT_BODY'=> urldecode(iconv('UTF-8','GB2312',$_POST['IPT_BODY'])),
               'jumpseq'=> '0',
                'mark'=>$_POST['mark']
            ]
        ]
    );
    echo $res->getStatusCode();
}

function getEmail($stuname)
{
    $emails = array();
    foreach (file('stuemail.csv') as $email)
    {
        $raw_email = explode(',',$email);
        $emails[$raw_email[0]] = $raw_email[1];
    }
    if ($emails[$stuname] == null) return false;
    else return $emails[$stuname];
}
